<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */	

defined ('IN_AGORA') or die;

class TaskController extends AgoraAdminController
{
	function _default()
	{
		$page = Agora::getVar('page','essentials');
		$this->view->template = 'options'.DS.$page;

		$model = & Model::getInstance('ConfigModel');
		$conf = $model->load();
		$this->view->assign('agora_config',$conf);

		if ($page === 'essentials') {
			$styles_model = & Model::getInstance('StylesModel');
		    $this->view->smarty->assign_by_ref('styles',$styles_model->loadAll());
		}
		if ($page === 'settings') {
		    $this->view->smarty->assign('attypes',array(0 => Agora::lang('Instant'), 1 => Agora::lang('Attachment popup')));
		}
		if ($page === 'disp') {
			$defaultview = array(
				'topic'=>JText::_('CC_TOPIC_VIEW'),
				'forum'=>JText::_('CC_FORUM_VIEW'),
			);
		
	    	$this->view->smarty->assign_by_ref('defaultview',$defaultview);
		}
	}

	function _save() {
	
		if (Agora::getRequestVar('action') === 'cancel') {
			$this->redirect('!task', '!action', '!page');
			return;
		}
	
		if (Agora::getRequestVar('action') === 'reload') {
			$this->redirect();
			return;
		}

		header('content-type: text/plain');

		$form = Agora::getPostVar('form',array(),true);

		$model = & Model::getInstance('ConfigModel');

		$conf = $model->load();
		$changed_data = array();
        $integration_flag = 0;
		
		foreach ($form as $var => $value) {
		
			if ($var === 'o_maintenance_message' || $var === 'o_rules_message') {
				$value = Agora::linebreaks($value);
			}
			
			if ($var === 'o_subscrb_auto_alladmins') {
			    $s_model = &Model::getInstance('SubscriptionModel');
			    $forum_model = &Model::getInstance('ForumModel');
			    $forums = $forum_model->loadAll();
			    if ($value) {
					$s_model->addAllMemRolToForums('admin',$forums);
                } else {
					$s_model->removeAllMemRolFromForums('admin',$forums);
				}
			}
			
			if ($var === 'o_subscrb_auto_moderators') {
			    $s_model = &Model::getInstance('SubscriptionModel');
			    if ($value) {
					$s_model->addAllMemRolToAllThereForums('moderator');
                } else {
					$s_model->removeAllMemRolToAllThereForums('moderator');
				}
			}
			
			if ($var === 'o_subscrb_auto_unapproved') {
				$s_model = &Model::getInstance('SubscriptionModel');
				if ($value) {
					$s_model->addAdminsToUnapproved();
				} else {
					$s_model->removeAdminsFromUnapproved();
				}
			}
			
			if ($conf[$var] != $value) {
				$changed_data[$var] = $value;
			}
			if (!$integration_flag)
			if ($var === 'o_pms_uddeim' || $var === 'o_community_builder' || $var === 'o_jomsocial' || $var === 'o_joomunity' || $var === 'o_joomsocialsuite')
			if ($value)
			{
			    $integration_flag++;
			    switch ($var)
			    {
			        case 'o_jomsocial':
			            
			    } 
			}
		}

		$model->save($changed_data);
		Agora::showMessage('Saved');
	
		if (Agora::getRequestVar('action') === 'apply') {
			$this->redirect();
			return;
		}
		$this->redirect('!task', '!action', '!page');
	}

}

